package com.aidex.web.controller.biz;

import com.aidex.biz.domain.EquInfo;
import com.aidex.biz.service.EquInfoService;
import com.aidex.biz.service.WeChatLoginService;
import com.aidex.common.annotation.Log;
import com.aidex.common.core.controller.BaseController;
import com.aidex.common.core.domain.R;
import com.aidex.common.core.page.PageDomain;
import com.aidex.common.enums.BusinessType;
import com.aidex.common.utils.SecurityUtils;
import com.aidex.common.utils.http.HttpUtils;
import com.aidex.common.utils.poi.ExcelUtil;
import com.aidex.system.service.CompanyService;
import com.alibaba.fastjson.JSONObject;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * 设备信息Controller
 *
 * @author sean
 * @email 2074844820@qq.com
 * @date 2022-09-22
 */
@RestController
@RequestMapping("/biz/bizEquInfo")
public class EquInfoController extends BaseController {
    @Autowired
    private EquInfoService equInfoService;
    @Autowired
    private CompanyService companyService;
    @Autowired
    private WeChatLoginService weChatLoginService;

    /**
     * 查询设备信息列表
     */
    @PreAuthorize("@ss.hasPermi('biz:bizEquInfo:list')")
    @GetMapping("/list")
    public R<PageInfo> list(EquInfo equInfo, HttpServletRequest request, HttpServletResponse response) {
        if (!SecurityUtils.isAdmin(SecurityUtils.getUserId())) {
            equInfo.setCompanyId(SecurityUtils.getLoginUser().getUser().getCompanyId());
        }
        equInfo.setStatus("-1");
        equInfo.setPage(new PageDomain(request, response));
        return R.data(equInfoService.findPage(equInfo));
    }

    /**
     * 获取设备信息详细信息
     */
    @PreAuthorize("@ss.hasPermi('biz:bizEquInfo:query')")
    @GetMapping(value = "/{id}")
    public R<EquInfo> detail(@PathVariable("id") String id) {
        return R.data(equInfoService.get(id));
    }

    /**
     * 新增设备信息
     */
    @PreAuthorize("@ss.hasPermi('biz:bizEquInfo:add')")
    @Log(title = "设备信息", businessType = BusinessType.INSERT)
    @PostMapping
    public R add(@RequestBody @Validated EquInfo equInfo) {
        return R.status(equInfoService.save(equInfo));
    }

    /**
     * 修改设备信息
     */
    @PreAuthorize("@ss.hasPermi('biz:bizEquInfo:edit')")
    @Log(title = "设备信息", businessType = BusinessType.UPDATE)
    @PutMapping
    public R edit(@RequestBody @Validated EquInfo equInfo) {
        return R.status(equInfoService.save(equInfo));
    }


    /**
     * 删除设备信息
     */
    @PreAuthorize("@ss.hasPermi('biz:bizEquInfo:remove')")
    @Log(title = "设备信息", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public R remove(@PathVariable String[] ids) {
        return R.status(equInfoService.deleteEquInfoByIds(ids));
    }

    /**
     * 获取最大编号
     */
    @PreAuthorize("@ss.hasPermi('biz:bizEquInfo:query')")
    @GetMapping("/findMaxSort")
    public R findMaxSort() {
        return R.data(equInfoService.findMaxSort(new EquInfo()));
    }

    /**
     * 导出设备信息列表
     */
    @PreAuthorize("@ss.hasPermi('biz:bizEquInfo:export')")
    @Log(title = "设备信息", businessType = BusinessType.EXPORT)
    @GetMapping("/export")
    public R export(EquInfo equInfo) {

        if (SecurityUtils.isAdmin(SecurityUtils.getUserId())) {
            equInfo.setCompanyId(null);
        } else {
            equInfo.setCompanyId(SecurityUtils.getLoginUser().getUser().getCompanyId());
        }
        List<EquInfo> list = equInfoService.findList(equInfo);
        ExcelUtil<EquInfo> util = new ExcelUtil<EquInfo>(EquInfo.class);
        return util.exportExcel(list, "设备信息数据");
    }

    @Log(title = "坐标地址转换", businessType = BusinessType.OTHER)
    @GetMapping("/getlocationBylatlng")
    public R getlocationBylatlng(String lat, String lng, String key) {
        String info = HttpUtils.sendGet("https://apis.map.qq.com/ws/geocoder/v1/", "location=" + lat + "," + lng + "&key=" + key + "&get_poi=1");
        return R.data(JSONObject.parse(info));
    }



}
